#include "iBe.h"
#include <iostream>

Mesh::Mesh(): 
  data1(NULL), data2(NULL), index(NULL),
  type(INVALID_TYPE),
  format(INVALID_FORMAT),
  data1_size(0), data2_size(0), index_size(0)
{}

Mesh::Mesh( const Handle & _handle, const String & _name )
{}

Mesh::~Mesh()
{
  if (data1) delete [] data1;
  if (data2) delete [] data2;
}

GLMesh::GLMesh():Mesh()
{}

GLMesh::GLMesh( const Handle & _handle, const String & _name ) :
  Mesh(_handle, _name)
{}

GLMesh::~GLMesh()
{}


bool GLMesh::setIndexedVertices( Turple3fArray & _vertexArray, Turple1iArray & _index )
{
  if (_vertexArray.size() == 0 || _vertexArray.size() % 2 != 0)
      return false;

  float        *data1_idx;
  unsigned int *index_idx;

  type   = TRIANGLES;
  format = VERTEX_COLOR_INTERLEAVE_INDEXED;
  
  data1_size = _vertexArray.size() / 2;
  data1_idx = data1 = new GLfloat[data1_size * 6 ];

  for( int i = 0; i < data1_size * 2; ++i )
    {
      *(data1_idx++) = _vertexArray[i][0];
      *(data1_idx++) = _vertexArray[i][1];
      *(data1_idx++) = _vertexArray[i][2]; 
    }

  index_size = _index.size(); 
  index_idx = index = new GLuint[index_size];

  for( int i = 0; i < index_size; ++i )
    {
      *(index_idx++) = _index[i][0];
    }

  return true;
}

Mesh2::Mesh2()
{
  size = 0;
  data = NULL;
}

Mesh2::~Mesh2()
{
  clear();
}

bool Mesh2::setIndexedVertices( Turple3fArray & _vertices, Turple1iArray & _index )
{
  return false;
}

void Mesh2::clear()
{
  size = 0;
  if (data) delete [] data;
}

void Mesh2::set( VertexArray & _vertexArray )
{
  if ( _vertexArray.size() == 0 )
    return;

  clear();
  
  size = _vertexArray.size();
  data = new float[ size * 6 ];

  float *idx = data;

  for(int i = 0; i < size; ++i)
    {
      *(idx++) = _vertexArray[i][0][0];
      *(idx++) = _vertexArray[i][0][1];
      *(idx++) = _vertexArray[i][0][2];
      *(idx++) = _vertexArray[i][1][0];
      *(idx++) = _vertexArray[i][1][1];
      *(idx++) = _vertexArray[i][1][2];
    }
}
